package com.sheng.center.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.sheng.center.entity.Student;

public class StudentDaoImpl implements StudentDao {
	private SessionFactory sessionFactory;
	@Override
	public void add(Student student) {
		student.setIsDelete(0);
		sessionFactory.getCurrentSession().save(student);

	}

	@Override
	public void edit(Student student) {
		student.setIsDelete(0);
		sessionFactory.getCurrentSession().update(student);
	}

	@Override
	public void delete(Student student) {
		student.setIsDelete(1);
		sessionFactory.getCurrentSession().update(student);
	}
	

	@SuppressWarnings("unchecked")
	@Override
	public List<Student> findAllList(Student model) {
		String hql = "from Student where 1=1  and is_delete=0";
		if(null != model.getUsername()){
			hql = hql + " and username like '%"+model.getUsername()+"%'";
		}
		Query query = sessionFactory.getCurrentSession().createQuery(hql);  
        List list = query.list();  
        return list;
	}
	

	public Student findById(Student student) {
		Query query = sessionFactory.getCurrentSession().createQuery("from Student where id='"+student.getId()+"'");  
		List list = query.list();  
		if (null != list && 0 < list.size()) {
			student = (Student) list.get(0);
		}
		return student;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public Student findByUsername(Student student) {
		Query query = sessionFactory.getCurrentSession().createQuery("from Student where username='"+student.getUsername()+"'");  
		List list = query.list();  
		if (null != list && 0 < list.size()) {
			student = (Student) list.get(0);
		}
		return student;
	}

}
